home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2007 September / PCWSEP07.iso / Software / Linux / Linux Mint 3.0 Light / LinuxMint-3.0-Light.iso / casper / filesystem.squashfs / usr / sbin / dpkg-statoverride < prev    next >
Encoding:
Text File  |  2007-03-06  |  5.9 KB  |  230 lines

  1. #! /usr/bin/perl
  2.  
  3. use POSIX;
  4. use POSIX qw(:errno_h :signal_h);
  5.  
  6. $admindir="/var/lib/dpkg"; # This line modified by Makefile
  7. $version="1.13.24"; # This line modified by Makefile
  8.  
  9. ($0) = $0 =~ m:.*/(.+):;
  10.  
  11. my $dpkglibdir="/usr/lib/dpkg"; # This line modified by Makefile
  12. push (@INC, $dpkglibdir);
  13. require 'dpkg-gettext.pl';
  14. textdomain("dpkg");
  15.  
  16. $verbose= 1;
  17. $doforce= 0;
  18. $doupdate= 0;
  19. $mode= "";
  20.  
  21. sub version {
  22.     printf _g("Debian %s version %s.\n"), $0, $version;
  23.  
  24.     printf _g("
  25. Copyright (C) 2000 Wichert Akkerman.");
  26.  
  27.     printf _g("
  28. This is free software; see the GNU General Public Licence version 2 or
  29. later for copying conditions. There is NO warranty.
  30. ");
  31. }
  32.  
  33. sub usage {
  34.     printf _g(
  35. "Usage: %s [<option> ...] <command>
  36.  
  37. Commands:
  38.   --add <owner> <group> <mode> <file>
  39.                            add a new entry into the database.
  40.   --remove <file>          remove file from the database.
  41.   --list [<glob-pattern>]  list current overrides in the database.
  42.  
  43. Options:
  44.   --admindir <directory>   set the directory with the statoverride file.
  45.   --update                 immediately update file permissions.
  46.   --force                  force an action even if a sanity check fails.
  47.   --quiet                  quiet operation, minimal output.
  48.   --help                   show this help message.
  49.   --version                show the version.
  50. "), $0;
  51. }
  52.  
  53. sub CheckModeConflict {
  54.     return unless $mode;
  55.     &badusage("two modes specified: $_ and --$mode");
  56. }
  57.  
  58. while (@ARGV) {
  59.     $_=shift(@ARGV);
  60.     last if m/^--$/;
  61.     if (!m/^-/) {
  62.         unshift(@ARGV,$_); last;
  63.     } elsif (m/^--help$/) {
  64.         &usage; exit(0);
  65.     } elsif (m/^--version$/) {
  66.         &version; exit(0);
  67.     } elsif (m/^--update$/) {
  68.         $doupdate=1;
  69.     } elsif (m/^--quiet$/) {
  70.         $verbose=0;
  71.     } elsif (m/^--force$/) {
  72.         $doforce=1;
  73.     } elsif (m/^--admindir$/) {
  74.         @ARGV || &badusage(sprintf(_g("--%s needs a <directory> argument"), "admindir"));
  75.         $admindir= shift(@ARGV);
  76.     } elsif (m/^--add$/) {
  77.         &CheckModeConflict;
  78.         $mode= 'add';
  79.     } elsif (m/^--remove$/) {
  80.         &CheckModeConflict;
  81.         $mode= 'remove';
  82.     } elsif (m/^--list$/) {
  83.         &CheckModeConflict;
  84.         $mode= 'list';
  85.     } else {
  86.         &badusage(sprintf(_g("unknown option \`%s'"), $_));
  87.     }
  88. }
  89.  
  90. $dowrite=0;
  91. $exitcode=0;
  92.  
  93. &badusage(_g("no mode specified")) unless $mode;
  94. &ReadOverrides;
  95.  
  96. if ($mode eq "add") {
  97.     @ARGV==4 || &badusage(_g("--add needs four arguments"));
  98.  
  99.     $user=$ARGV[0];
  100.     if ($user =~ m/^#([0-9]+)$/) {
  101.         $uid=$1;
  102.         &badusage(sprintf(_g("illegal user %s"), $user)) if ($uid<0);
  103.     } else {
  104.         (($name,$pw,$uid)=getpwnam($user)) || &badusage(sprintf(_g("non-existing user %s"), $user));
  105.     }
  106.  
  107.     $group=$ARGV[1];
  108.     if ($group =~ m/^#([0-9]+)$/) {
  109.         $gid=$1;
  110.         &badusage(sprintf(_g("illegal group %s"), $group)) if ($gid<0);
  111.     } else {
  112.         (($name,$pw,$gid)=getgrnam($group)) || &badusage(sprintf(_g("non-existing group %s"), $group));
  113.     }
  114.  
  115.     $mode= $ARGV[2];
  116.     (($mode<0) or (oct($mode)>07777) or ($mode !~ m/\d+/)) && &badusage(sprintf(_g("illegal mode %s"), $mode));
  117.     $file= $ARGV[3];
  118.     $file =~ m/\n/ && &badusage(_g("file may not contain newlines"));
  119.     $file =~ s,/+$,, && print STDERR _g("stripping trailing /")."\n";
  120.  
  121.     if (defined $owner{$file}) {
  122.         printf STDERR _g("An override for \"%s\" already exists, "), $file;
  123.         if ($doforce) {
  124.             print STDERR _g("but --force specified so will be ignored.")."\n";
  125.         } else {
  126.             print STDERR _g("aborting")."\n";
  127.             exit(3);
  128.         }
  129.     }
  130.     $owner{$file}=$user;
  131.     $group{$file}=$group;
  132.     $mode{$file}=$mode;
  133.     $dowrite=1;
  134.  
  135.     if ($doupdate) {
  136.         if (not -e $file) {
  137.         printf STDERR _g("warning: --update given but %s does not exist")."\n", $file;
  138.         } else {
  139.         chown ($uid,$gid,$file) || warn sprintf(_g("failed to chown %s: %s"), $file, $!)."\n";
  140.         chmod (oct($mode),$file) || warn sprintf(_g("failed to chmod %s: %s"), $file, $!)."\n";
  141.         }
  142.     }
  143. } elsif ($mode eq "remove") {
  144.     @ARGV==1 || &badusage(sprintf(_g("--%s needs a single argument"), "remove"));
  145.     $file=$ARGV[0];
  146.     $file =~ s,/+$,, && print STDERR _g("stripping trailing /")."\n";
  147.     if (not defined $owner{$file}) {
  148.         print STDERR _g("No override present.")."\n";
  149.         exit(0) if ($doforce); 
  150.         exit(2);
  151.     }
  152.     delete $owner{$file};
  153.     delete $group{$file};
  154.     delete $mode{$file};
  155.     $dowrite=1;
  156.     print(STDERR _g("warning: --update is useless for --remove")."\n") if ($doupdate);
  157. } elsif ($mode eq "list") {
  158.     my (@list,@ilist,$pattern,$file);
  159.     
  160.     @ilist= @ARGV ? @ARGV : ('*');
  161.     while (defined($_=shift(@ilist))) {
  162.         s/\W/\\$&/g;
  163.         s/\\\?/./g;
  164.         s/\\\*/.*/g;
  165.         s,/+$,, && print STDERR _g("stripping trailing /")."\n";
  166.         push(@list,"^$_\$");
  167.     }
  168.     $pat= join('|',@list);
  169.     $exitcode=1;
  170.     for $file (keys %owner) {
  171.         next unless ($file =~ m/$pat/o);
  172.         $exitcode=0;
  173.         print "$owner{$file} $group{$file} $mode{$file} $file\n";
  174.     }
  175. }
  176.  
  177. &WriteOverrides if ($dowrite);
  178.  
  179. exit($exitcode);
  180.  
  181. sub ReadOverrides {
  182.     open(SO,"$admindir/statoverride") || &quit(sprintf(_g("cannot open statoverride: %s"), $!));
  183.     while (<SO>) {
  184.         my ($owner,$group,$mode,$file);
  185.         chomp;
  186.  
  187.         ($owner,$group,$mode,$file)=split(' ', $_, 4);
  188.         die sprintf(_g("Multiple overrides for \"%s\", aborting"), $file)
  189.             if defined $owner{$file};
  190.         $owner{$file}=$owner;
  191.         $group{$file}=$group;
  192.         $mode{$file}=$mode;
  193.     }
  194.     close(SO);
  195. }
  196.  
  197.  
  198. sub WriteOverrides {
  199.     my ($file);
  200.  
  201.     open(SO,">$admindir/statoverride-new") || &quit(sprintf(_g("cannot open new statoverride file: %s"), $!));
  202.     foreach $file (keys %owner) {
  203.         print SO "$owner{$file} $group{$file} $mode{$file} $file\n";
  204.     }
  205.     close(SO);
  206.     chmod(0644, "$admindir/statoverride-new");
  207.     unlink("$admindir/statoverride-old") ||
  208.         $! == ENOENT || &quit(sprintf(_g("error removing statoverride-old: %s"), $!));
  209.     link("$admindir/statoverride","$admindir/statoverride-old") ||
  210.         $! == ENOENT || &quit(sprintf(_g("error creating new statoverride-old: %s"), $!));
  211.     rename("$admindir/statoverride-new","$admindir/statoverride")
  212.         || &quit(sprintf(_g("error installing new statoverride: %s"), $!));
  213. }
  214.  
  215.  
  216. sub quit
  217. {
  218.     printf STDERR "%s: %s\n", $0, "@_";
  219.     exit(2);
  220. }
  221.  
  222. sub badusage
  223. {
  224.     printf STDERR "%s: %s\n\n", $0, "@_";
  225.     &usage;
  226.     exit(2);
  227. }
  228.  
  229. # vi: ts=8 sw=8 ai si cindent
  230.